<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
  <meta name="renderer" content="webkit">

  
  <title>Android 10 音量UI更新解析 | 龙之叶的博客</title>

  <link rel="shortcut icon" href="/images/favicon.png">
  <link rel="alternate" href="/atom.xml" title="龙之叶的博客" type="application/atom+xml">
  <meta name="description" content="Android 10 音量UI更新解析">
<meta property="og:type" content="article">
<meta property="og:title" content="Android 10 音量UI更新解析">
<meta property="og:url" content="http://longzhiye.top/2024/01/07/2024-01-07/index.html">
<meta property="og:site_name" content="龙之叶的博客">
<meta property="og:description" content="Android 10 音量UI更新解析">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2024-01-06T16:00:00.000Z">
<meta property="article:modified_time" content="2024-08-29T02:40:34.919Z">
<meta property="article:author" content="龙之叶">
<meta property="article:tag" content="Android">
<meta property="article:tag" content="Settings">
<meta property="article:tag" content="framework">
<meta property="article:tag" content="Launcher">
<meta name="twitter:card" content="summary">

  <meta name="keywords" content=",Android,Settings,framework,Launcher">
  <meta name="format-detection" content="telephone=no,email=no">
  <meta name="theme-color" content="#9C27B0">

  <meta name="mobile-web-app-capable" content="yes">
  <meta name="application-name" content="龙之叶的博客">
  <meta name="msapplication-starturl" content="http://longzhiye.top/2024/01/07/2024-01-07/">
  <meta name="msapplication-navbutton-color" content="#9C27B0">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-title" content="龙之叶的博客">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <link rel="apple-touch-icon" href="/images/favicon.png">

  
    <link rel="canonical" href="http://longzhiye.top/2024/01/07/2024-01-07/">
  

  
  

  
  
  

  
<link rel="stylesheet" href="/css/mdui.css">
<link rel="stylesheet" href="/css/style.css">

  
<link rel="stylesheet" href="/custom.css">

<meta name="generator" content="Hexo 5.4.2"></head>

<body class="mdui-appbar-with-toolbar mdui-theme-primary-indigo mdui-theme-accent-pink">
  <script>var a=localStorage.getItem("mdui-theme-layout-dark");if(a){document.getElementsByTagName("body")[0].className+=" mdui-theme-layout-dark"};</script>
  <script>if(window.matchMedia&&window.matchMedia("(prefers-color-scheme: dark)").matches){document.getElementsByTagName("body")[0].className+=" mdui-theme-layout-dark"};</script>
  <script>var a=localStorage.getItem("mdui-drawer-close");if(!a){document.getElementsByTagName("body")[0].className+=" mdui-drawer-body-left"};</script>
  <header id="header" class="mdui-appbar mdui-appbar-fixed mdui-appbar-scroll-hide mdui-appbar-inset">
  <div class="mdui-toolbar mdui-color-theme">
    <a href="javascript:;" class="mdui-btn mdui-btn-icon" mdui-drawer="{target: '#sidebar', swipe: true}"><i class="mdui-icon material-icons">menu</i></a>
    <a href="/" class="mdui-typo-headline">龙之叶的博客</a>
    <div class="mdui-toolbar-spacer"></div>
    <a href="javascript:;" class="mdui-btn mdui-btn-icon" mdui-dialog="{target: '#search'}" mdui-tooltip="{content: '搜索'}"><i class="mdui-icon material-icons">search</i></a>
	<!--
    <a href="/atom.xml" class="mdui-btn mdui-btn-icon" mdui-tooltip="{content: 'RSS'}"><i class="mdui-icon material-icons">rss_feed</i></a>
	-->
  </div>
</header>
<div class="mdui-dialog" id="search">
  
    <div class="search-form">
      <input type="search" class="search-form-input" placeholder="请输入关键字">
    </div>
    <div class="search-result" data-resource="/search.xml"></div>
  
</div>

  <aside id="sidebar" class="mdui-drawer mdui-drawer-full-height">
  <script>var a=localStorage.getItem("mdui-drawer-close");if(a){document.getElementById("sidebar").className+=" mdui-drawer-close"};</script>
  <div class="mdui-grid-tile">
    <img src="/images/banner.png" style="height: 160px;">
    <img src="/images/avatar.png" class="avatar-animation" style="position: absolute; top: 10%; left: 24px; width: 64px; height: 64px; border: 2px solid #fff; border-radius: 50%;background-color:#fff;">
    <div class="mdui-grid-tile-actions">
      <div class="mdui-grid-tile-text">
        <div class="mdui-grid-tile-title">龙之叶</div>
        <div class="mdui-grid-tile-subtitle"><i class="mdui-icon material-icons">art_track</i>生命不息，折腾不止</div>
      </div>
      
        <div class="mdui-grid-tile-buttons">
          <a href="mailto:longzhiye163@163.com" class="mdui-btn mdui-btn-icon" mdui-tooltip="{content: 'longzhiye163@163.com'}"><i class="mdui-icon material-icons">email</i></a>
        </div>
      
    </div>
  </div>

  <div class="mdui-list" mdui-collapse="{accordion: true}">
    <a href="/" class="mdui-list-item mdui-ripple">
      <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-blue">home</i>
      <div class="mdui-list-item-content">主页</div>
    </a>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-0");if(a){document.getElementsByClassName("mdui-collapse-item")[0].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-deep-orange">archive</i>
        <div class="mdui-list-item-content">归档</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/archives/2025/07/">七月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/06/">六月 2025<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/03/">三月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/02/">二月 2025<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/01/">一月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/10/">十月 2024<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/09/">九月 2024<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/08/">八月 2024<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/03/">三月 2024<span class="mdui-ripple sidebar_archives-count">5</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/02/">二月 2024<span class="mdui-ripple sidebar_archives-count">11</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/01/">一月 2024<span class="mdui-ripple sidebar_archives-count">9</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/12/">十二月 2023<span class="mdui-ripple sidebar_archives-count">10</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/11/">十一月 2023<span class="mdui-ripple sidebar_archives-count">8</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/10/">十月 2023<span class="mdui-ripple sidebar_archives-count">8</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/09/">九月 2023<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/08/">八月 2023<span class="mdui-ripple sidebar_archives-count">6</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/06/">六月 2023<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/05/">五月 2023<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/04/">四月 2023<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/03/">三月 2023<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/02/">二月 2023<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2022/09/">九月 2022<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2022/08/">八月 2022<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/04/">四月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/03/">三月 2017<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/02/">二月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/01/">一月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/12/">十二月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/11/">十一月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/10/">十月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/08/">八月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a>
        
      </div>
    </div>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-1");if(a){document.getElementsByClassName("mdui-collapse-item")[1].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-green">class</i>
        <div class="mdui-list-item-content">分类</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/categories/%E6%8A%80%E6%9C%AF/">技术<span class="mdui-ripple sidebar_archives-count">98</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E7%94%9F%E6%B4%BB/">生活<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E8%B5%84%E8%AE%AF/">资讯<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E9%80%9A%E7%9F%A5/">通知<span class="mdui-ripple sidebar_archives-count">4</span></a>
        
      </div>
    </div>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-2");if(a){document.getElementsByClassName("mdui-collapse-item")[2].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-brown">bookmark</i>
        <div class="mdui-list-item-content">标签</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/tags/ANR/" rel="tag">ANR<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/APP/" rel="tag">APP<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android/" rel="tag">Android<span class="mdui-ripple sidebar_archives-count">89</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android12/" rel="tag">Android12<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android13/" rel="tag">Android13<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android14/" rel="tag">Android14<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/AndroidStudio/" rel="tag">AndroidStudio<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/AutoDraw/" rel="tag">AutoDraw<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Crash/" rel="tag">Crash<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Foxmail/" rel="tag">Foxmail<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Framework/" rel="tag">Framework<span class="mdui-ripple sidebar_archives-count">20</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/GPS%E8%BD%A8%E8%BF%B9%E5%9B%BE/" rel="tag">GPS轨迹图<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Git/" rel="tag">Git<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/GitHub/" rel="tag">GitHub<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Google/" rel="tag">Google<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Hexo/" rel="tag">Hexo<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Java/" rel="tag">Java<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Kernel/" rel="tag">Kernel<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Launcher/" rel="tag">Launcher<span class="mdui-ripple sidebar_archives-count">17</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Lib/" rel="tag">Lib<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Linux/" rel="tag">Linux<span class="mdui-ripple sidebar_archives-count">18</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Markdown/" rel="tag">Markdown<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Material-Design/" rel="tag">Material Design<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/OS-X/" rel="tag">OS X<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/OTA/" rel="tag">OTA<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/QuickTime/" rel="tag">QuickTime<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/SSH/" rel="tag">SSH<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Settings/" rel="tag">Settings<span class="mdui-ripple sidebar_archives-count">6</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/SystemUI/" rel="tag">SystemUI<span class="mdui-ripple sidebar_archives-count">19</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Ubuntu/" rel="tag">Ubuntu<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/WPS/" rel="tag">WPS<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Wifi/" rel="tag">Wifi<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/WindTerm/" rel="tag">WindTerm<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Windows/" rel="tag">Windows<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/androidmk/" rel="tag">androidmk<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/blog/" rel="tag">blog<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/build/" rel="tag">build<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/feel/" rel="tag">feel<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/framework/" rel="tag">framework<span class="mdui-ripple sidebar_archives-count">50</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/git/" rel="tag">git<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/inotify/" rel="tag">inotify<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/life/" rel="tag">life<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/ninja/" rel="tag">ninja<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/rsync/" rel="tag">rsync<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/standard/" rel="tag">standard<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/test/" rel="tag">test<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E4%BC%81%E4%B8%9A%E5%BE%AE%E4%BF%A1/" rel="tag">企业微信<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" rel="tag">操作系统<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%B5%8B%E8%AF%95/" rel="tag">测试<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%BA%90%E7%A0%81/" rel="tag">源码<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E7%94%9F%E6%B4%BB%E6%84%9F%E6%82%9F/" rel="tag">生活感悟<span class="mdui-ripple sidebar_archives-count">1</span></a>
        
      </div>
    </div>
    <a href="/about" class="mdui-list-item mdui-ripple">
      <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-purple">person</i>
      <div class="mdui-list-item-content">关于</div>
    </a>
  </div>

  <div class="mdui-divider"></div>

  <div class="mdui-list" mdui-collapse="{accordion: true}">
    
      <a href="/timeline" class="mdui-list-item mdui-ripple">时间轴</a>
    
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-3");if(a){document.getElementsByClassName("mdui-collapse-item")[3].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <div class="mdui-list-item-content">友情链接</div>
        <i class="mdui-list-item-icon mdui-icon material-icons">link</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
          <a href="http://www.niemingzhao.top" target="_blank" class="mdui-list-item mdui-p-l-2 mdui-text-color-theme-accent mdui-ripple" style="justify-content: center;">聂明照的博客</a>
        
        
      </div>
    </div>
  </div>
</aside>

  <main id="main" class="mdui-m-t-5 fadeIn animated">
  
<link rel="stylesheet" href="//cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css">

  <style>#main article .mdui-card-content .center-block{display:block!important;margin-right:auto!important;margin-left:auto!important}</style>
  <style>#main article .mdui-card-content .text-center{text-align:center!important}</style>
  <article class="mdui-card mdui-m-b-5">
    <header class="mdui-card-media">
      <img src="/images/random/material-16.png" style="max-height: 240px;">
      <div class="mdui-card-media-covered">
        <div class="mdui-card-primary">
          <div class="mdui-card-primary-title">Android 10 音量UI更新解析</div>
          <div class="mdui-card-primary-subtitle"><i class="iconfont">&#xe697;</i> 2024-01-07 / <i class="iconfont">&#xe601;</i> 龙之叶 &nbsp;&nbsp; <span id="busuanzi_container_page_pv" style="display: none;"><i class="iconfont">&#xe7fd;</i> <span id="busuanzi_value_page_pv"></span></span></div>
        </div>
      </div>
      <div class="mdui-card-menu">
        
          <button class="mdui-btn mdui-btn-icon mdui-text-color-white" mdui-menu="{target: '#qrcode', align: 'right'}"><i class="mdui-icon material-icons">devices</i></button>
          <ul class="mdui-menu" id="qrcode">
            
              <li class="mdui-menu-item"><a class="mdui-text-center mdui-ripple">发送到手机</a></li>
            
            <li class="mdui-menu-item" disabled>
              
                <img src="//qr.liantu.com/api.php?w=246&m=10&text=http://longzhiye.top/2024/01/07/2024-01-07/">
              
            </li>
          </ul>
        
        
          <button class="mdui-btn mdui-btn-icon mdui-text-color-white" mdui-menu="{target: '#share_menu', align: 'right'}"><i class="mdui-icon material-icons">share</i></button>
          <ul class="mdui-menu" id="share_menu">
            <li class="mdui-menu-item">
              <a href="https://service.weibo.com/share/share.php?appkey=&title=Android 10 音量UI更新解析&url=http://longzhiye.top/2024/01/07/2024-01-07/&pic=http://longzhiye.top/images/favicon.png&searchPic=false&style=simple" target="_blank" class="mdui-ripple">分享到 Weibo</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://twitter.com/intent/tweet?text=Android 10 音量UI更新解析&url=http://longzhiye.top/2024/01/07/2024-01-07/&via=龙之叶" target="_blank" class="mdui-ripple">分享到 Twitter</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://www.facebook.com/sharer/sharer.php?u=http://longzhiye.top/2024/01/07/2024-01-07/" target="_blank" class="mdui-ripple">分享到 Facebook</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://plus.google.com/share?url=http://longzhiye.top/2024/01/07/2024-01-07/" target="_blank" class="mdui-ripple">分享到 Google+</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://www.linkedin.com/shareArticle?mini=true&url=http://longzhiye.top/2024/01/07/2024-01-07/&title=Android 10 音量UI更新解析" target="_blank" class="mdui-ripple">分享到 LinkedIn</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://connect.qq.com/widget/shareqq/index.html?site=龙之叶的博客&title=Android 10 音量UI更新解析&pics=http://longzhiye.top/images/favicon.png&url=http://longzhiye.top/2024/01/07/2024-01-07/" target="_blank" class="mdui-ripple">分享到 QQ</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://telegram.me/share/url?url=http://longzhiye.top/2024/01/07/2024-01-07/&text=Android 10 音量UI更新解析" target="_blank" class="mdui-ripple">分享到 Telegram</a>
            </li>
          </ul>
        
      </div>
    </header>
    <div class="mdui-card-content mdui-typo">
      <h4 id="1-VolumeUI-的启动"><a href="#1-VolumeUI-的启动" class="headerlink" title="1 VolumeUI 的启动"></a>1 VolumeUI 的启动</h4><p>由于VolumeUI 是继承 SystemUI 的，所以它的启动方式和 SystemUI 的启动方式一样。<br>直接看 VolumeUI 的start()方法<br><strong>frameworks/base/packages/SystemUI/src/com/android/systemui/volume/VolumeUI.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">start</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="type">boolean</span> <span class="variable">enableVolumeUi</span> <span class="operator">=</span> mContext.getResources().getBoolean(R.bool.enable_volume_ui);</span><br><span class="line">    <span class="type">boolean</span> <span class="variable">enableSafetyWarning</span> <span class="operator">=</span></span><br><span class="line">        mContext.getResources().getBoolean(R.bool.enable_safety_warning);</span><br><span class="line">    mEnabled = enableVolumeUi || enableSafetyWarning;</span><br><span class="line">    <span class="keyword">if</span> (!mEnabled) <span class="keyword">return</span>;</span><br><span class="line">    mVolumeComponent.setEnableDialogs(enableVolumeUi, enableSafetyWarning);</span><br><span class="line">    setDefaultVolumeController();</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">setDefaultVolumeController</span><span class="params">()</span> &#123;</span><br><span class="line">    DndTile.setVisible(mContext, <span class="literal">true</span>);</span><br><span class="line">    <span class="keyword">if</span> (D.BUG) Log.d(TAG, <span class="string">&quot;Registering default volume controller&quot;</span>);</span><br><span class="line">    mVolumeComponent.register();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>VolumeUI 启动的时候做了一些初始化的操作、并且会创建一个 VolumeDialogComponent 对象，从名字可以看出，它代表 VolumeUI 组件，通过它可以创建整个MVP。<br>VolumeDialogComponent 对象创建完成后，就会调用它的register()方法启动 VolumeUI 功能。它其实就是关联 Presenter 层和 Model 层。<br>两件事情：</p>
<ol>
<li>VolumeDialogComponent里面会去创建我们的音量条UI的实例对象，也就是VolumeDialogImpl。</li>
<li>setDefaultVolumeController方法会设置AudioService的回调接口。</li>
</ol>
<h4 id="2-创建VolumeDialogImpl"><a href="#2-创建VolumeDialogImpl" class="headerlink" title="2 创建VolumeDialogImpl"></a>2 创建VolumeDialogImpl</h4><p>首先来看看 VolumeDialogComponent 的构造函数：<br><strong>frameworks/base/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogComponent.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Inject</span></span><br><span class="line"><span class="keyword">public</span> <span class="title function_">VolumeDialogComponent</span><span class="params">(Context context, KeyguardViewMediator keyguardViewMediator,</span></span><br><span class="line"><span class="params">        VolumeDialogControllerImpl volumeDialogController)</span> &#123;</span><br><span class="line">    mContext = context;</span><br><span class="line">    mKeyguardViewMediator = keyguardViewMediator;</span><br><span class="line">    mController = volumeDialogController;</span><br><span class="line">    mController.setUserActivityListener(<span class="built_in">this</span>);</span><br><span class="line">    <span class="comment">// Allow plugins to reference the VolumeDialogController.</span></span><br><span class="line">    Dependency.get(PluginDependencyProvider.class)</span><br><span class="line">            .allowPluginDependency(VolumeDialogController.class);</span><br><span class="line">    Dependency.get(ExtensionController.class).newExtension(VolumeDialog.class)</span><br><span class="line">            .withPlugin(VolumeDialog.class)</span><br><span class="line">            .withDefault(<span class="built_in">this</span>::createDefault)</span><br><span class="line">            .withCallback(dialog -&gt; &#123;</span><br><span class="line">                <span class="keyword">if</span> (mDialog != <span class="literal">null</span>) &#123;</span><br><span class="line">                    mDialog.destroy();</span><br><span class="line">                &#125;</span><br><span class="line">                mDialog = dialog;</span><br><span class="line">                mDialog.init(LayoutParams.TYPE_VOLUME_OVERLAY, mVolumeDialogCallback);</span><br><span class="line">            &#125;).build();</span><br><span class="line">    applyConfiguration();</span><br><span class="line">    Dependency.get(TunerService.class).addTunable(<span class="built_in">this</span>, VOLUME_DOWN_SILENT, VOLUME_UP_SILENT,</span><br><span class="line">            VOLUME_SILENT_DO_NOT_DISTURB);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">protected</span> VolumeDialog <span class="title function_">createDefault</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="type">VolumeDialogImpl</span> <span class="variable">impl</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">VolumeDialogImpl</span>(mContext);</span><br><span class="line">    impl.setStreamImportant(AudioManager.STREAM_SYSTEM, <span class="literal">false</span>);</span><br><span class="line">    impl.setAutomute(<span class="literal">true</span>);</span><br><span class="line">    impl.setSilentMode(<span class="literal">false</span>);</span><br><span class="line">    <span class="keyword">return</span> impl;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>VolumeDialogComponent 通过 createDefault() 创建 VolumeDialogImpl 对象，它代表 View 层，然后通过init() 进行了初始化。<br><strong>frameworks/base/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogImpl.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">  <span class="keyword">public</span> <span class="title function_">VolumeDialogImpl</span><span class="params">(Context context)</span> &#123;</span><br><span class="line">      <span class="comment">// VolumeDialogControllerImpl</span></span><br><span class="line">      mController = Dependency.get(VolumeDialogController.class);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">init</span><span class="params">(<span class="type">int</span> windowType, Callback callback)</span> &#123;</span><br><span class="line">      initDialog();</span><br><span class="line">      mAccessibility.init();</span><br><span class="line">      mController.addCallback(mControllerCallbackH, mHandler);</span><br><span class="line">      mController.getState();</span><br><span class="line">      Dependency.get(ConfigurationController.class).addCallback(<span class="built_in">this</span>);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">final</span> VolumeDialogController.<span class="type">Callbacks</span> <span class="variable">mControllerCallbackH</span></span><br><span class="line">          <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">VolumeDialogController</span>.Callbacks() &#123;</span><br><span class="line">      <span class="meta">@Override</span></span><br><span class="line">      <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onStateChanged</span><span class="params">(State state)</span> &#123;</span><br><span class="line">          onStateChangedH(state);</span><br><span class="line">      &#125;</span><br><span class="line">      ...</span><br><span class="line">  &#125;;</span><br><span class="line">        </span><br><span class="line"><span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">class</span> <span class="title class_">H</span> <span class="keyword">extends</span> <span class="title class_">Handler</span> &#123;</span><br><span class="line">      ...</span><br><span class="line">      <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">int</span> <span class="variable">STATE_CHANGED</span> <span class="operator">=</span> <span class="number">7</span>;</span><br><span class="line">      <span class="keyword">public</span> <span class="title function_">H</span><span class="params">()</span> &#123;</span><br><span class="line">          <span class="built_in">super</span>(Looper.getMainLooper());</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="meta">@Override</span></span><br><span class="line">      <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">handleMessage</span><span class="params">(Message msg)</span> &#123;</span><br><span class="line">          <span class="keyword">switch</span> (msg.what) &#123;</span><br><span class="line">              ...</span><br><span class="line">              <span class="keyword">case</span> STATE_CHANGED: onStateChangedH(mState); <span class="keyword">break</span>;</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  ...</span><br></pre></td></tr></table></figure>
<p>在 VolumeDialogImpl (View层)的构造函数中，创建了 VolumeDialogControllerImpl 对象，它代表了 Presenter 层。<br>在 init() 中，会向 VolumeDialogControllerImpl (Presenter层) 注册一个回调，也就是 View 层与 Presenter 层建立关联，从而可以通过 Presenter 层控制 View 层。<br>现在 View 层已经和 Presenter 层关联了，那么 Model 层呢？还记得前面提到的启动 VolumeUI 功能的代码吗？它调用的是 VolumeDialogComponent#register()，它完成的就是 Model 层与 Presenter 的关联，具体调用的是 VolumeDialogControllerImpl#register()。<br>这一段代码做了如下几件事情：</p>
<ol>
<li>初始化dialog，设置dialog的布局等等。</li>
<li>添加VolumeDialogController的回调，当VolumeDialogController接收到AudioService的回调之后，通过Callback将事件继续通知给Dialog去做出响应的处理。这里的两个参数，一个是回调各个状态的接口，一个是在主线程初始化的Handler。</li>
</ol>
<p>通过init()方法里的 mController.addCallback(mControllerCallbackH, mHandler)；进入到VolumeDialogControllerImpl，代码如下：</p>
<p><strong>frameworks/base/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogControllerImpl.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Singleton</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">VolumeDialogControllerImpl</span> <span class="keyword">implements</span> <span class="title class_">VolumeDialogController</span>, Dumpable &#123;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">protected</span> <span class="type">C</span> <span class="variable">mCallbacks</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">C</span>();</span><br><span class="line">    ...</span><br><span class="line">    <span class="comment">// 添加回调监听</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">addCallback</span><span class="params">(Callbacks callback, Handler handler)</span> &#123;</span><br><span class="line">        mCallbacks.add(callback, handler);</span><br><span class="line">        callback.onAccessibilityModeChanged(mShowA11yStream);</span><br><span class="line">    &#125;</span><br><span class="line">  </span><br><span class="line">    <span class="keyword">class</span> <span class="title class_">C</span> <span class="keyword">implements</span> <span class="title class_">Callbacks</span> &#123;</span><br><span class="line">        <span class="comment">// Callbacks作为key，Handler为value</span></span><br><span class="line">        <span class="keyword">private</span> <span class="keyword">final</span> HashMap&lt;Callbacks, Handler&gt; mCallbackMap = <span class="keyword">new</span> <span class="title class_">HashMap</span>&lt;&gt;();</span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">add</span><span class="params">(Callbacks callback, Handler handler)</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (callback == <span class="literal">null</span> || handler == <span class="literal">null</span>) <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">IllegalArgumentException</span>();</span><br><span class="line">            mCallbackMap.put(callback, handler);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onStateChanged</span><span class="params">(<span class="keyword">final</span> State state)</span> &#123;</span><br><span class="line">            <span class="keyword">final</span> <span class="type">long</span> <span class="variable">time</span> <span class="operator">=</span> System.currentTimeMillis();</span><br><span class="line">            <span class="keyword">final</span> <span class="type">State</span> <span class="variable">copy</span> <span class="operator">=</span> state.copy();</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">final</span> Map.Entry&lt;Callbacks, Handler&gt; entry : mCallbackMap.entrySet()) &#123;</span><br><span class="line">                entry.getValue().post(<span class="keyword">new</span> <span class="title class_">Runnable</span>() &#123;</span><br><span class="line">                    <span class="meta">@Override</span></span><br><span class="line">                    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">run</span><span class="params">()</span> &#123;</span><br><span class="line">                        entry.getKey().onStateChanged(copy);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;);</span><br><span class="line">            &#125;</span><br><span class="line">            Events.writeState(time, copy);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  </span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这里C是Callbacks的实现类，并且在内部有一个Map，用来存放对应的Callbacks以及Handler<br>在VolumeDialogControllerImpl收到来自AudioService的方法之后，就会调用mCallbacks的方法，由于调用的地方是在工作线程，所以在这里通过Handler转化为了UI线程去调用，在对应的实现地方就可以直接改变UI了。<br>Callbacks代码如下：<br><strong>frameworks/base/packages/SystemUI/plugin/src/com/android/systemui/plugins/VolumeDialogController.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@ProvidesInterface(version = VolumeDialogController.VERSION)</span></span><br><span class="line"><span class="meta">@DependsOn(target = StreamState.class)</span></span><br><span class="line"><span class="meta">@DependsOn(target = State.class)</span></span><br><span class="line"><span class="meta">@DependsOn(target = Callbacks.class)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">VolumeDialogController</span> &#123;</span><br><span class="line">    <span class="meta">@ProvidesInterface(version = Callbacks.VERSION)</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">Callbacks</span> &#123;</span><br><span class="line">        <span class="type">int</span> <span class="variable">VERSION</span> <span class="operator">=</span> <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">void</span> <span class="title function_">onShowRequested</span><span class="params">(<span class="type">int</span> reason)</span>;</span><br><span class="line">        <span class="keyword">void</span> <span class="title function_">onDismissRequested</span><span class="params">(<span class="type">int</span> reason)</span>;</span><br><span class="line">        <span class="keyword">void</span> <span class="title function_">onStateChanged</span><span class="params">(State state)</span>;</span><br><span class="line">        <span class="keyword">void</span> <span class="title function_">onLayoutDirectionChanged</span><span class="params">(<span class="type">int</span> layoutDirection)</span>;</span><br><span class="line">        <span class="keyword">void</span> <span class="title function_">onConfigurationChanged</span><span class="params">()</span>;</span><br><span class="line">        <span class="keyword">void</span> <span class="title function_">onShowVibrateHint</span><span class="params">()</span>;</span><br><span class="line">        <span class="keyword">void</span> <span class="title function_">onShowSilentHint</span><span class="params">()</span>;</span><br><span class="line">        <span class="keyword">void</span> <span class="title function_">onScreenOff</span><span class="params">()</span>;</span><br><span class="line">        <span class="keyword">void</span> <span class="title function_">onShowSafetyWarning</span><span class="params">(<span class="type">int</span> flags)</span>;</span><br><span class="line">        <span class="keyword">void</span> <span class="title function_">onAccessibilityModeChanged</span><span class="params">(Boolean showA11yStream)</span>;</span><br><span class="line">        <span class="keyword">void</span> <span class="title function_">onCaptionComponentStateChanged</span><span class="params">(Boolean isComponentEnabled, Boolean fromTooltip)</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="3-注册VolumeController"><a href="#3-注册VolumeController" class="headerlink" title="3 注册VolumeController"></a>3 注册VolumeController</h4><p>接着来看setDefaultVolumeController，这个比较重要：<br><strong>frameworks/base/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogComponent.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Singleton</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">VolumeDialogComponent</span> <span class="keyword">implements</span> <span class="title class_">VolumeComponent</span>, TunerService.Tunable,</span><br><span class="line">        VolumeDialogControllerImpl.UserActivityListener&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> VolumeDialogControllerImpl mController;</span><br><span class="line">    </span><br><span class="line">    <span class="meta">@Inject</span></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">VolumeDialogComponent</span><span class="params">(Context context, KeyguardViewMediator keyguardViewMediator,</span></span><br><span class="line"><span class="params">            VolumeDialogControllerImpl volumeDialogController)</span> &#123;</span><br><span class="line">        mController = volumeDialogController;</span><br><span class="line">        ...</span><br><span class="line">    &#125;</span><br><span class="line">          </span><br><span class="line">    ...  </span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">register</span><span class="params">()</span> &#123;</span><br><span class="line">        mController.register();</span><br><span class="line">    &#125;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>VolumeDialogComponent调用VolumeDialogControllerImpl的方法：<br><strong>frameworks/base/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogControllerImpl.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">protected</span> <span class="keyword">final</span> <span class="type">VC</span> <span class="variable">mVolumeController</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">VC</span>();</span><br><span class="line"> </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">register</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        <span class="comment">// 向Audio Manager注册了一个Binder，其实就是一个回调</span></span><br><span class="line">        setVolumeController();</span><br><span class="line">        setVolumePolicy(mVolumePolicy);</span><br><span class="line">        showDndTile(mShowDndTile);</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        mMediaSessions.init();</span><br><span class="line">    &#125; <span class="keyword">catch</span> (SecurityException e) &#123;</span><br><span class="line">        Log.w(TAG, <span class="string">&quot;No access to media sessions&quot;</span>, e);</span><br><span class="line">    &#125;</span><br><span class="line">    &#125; <span class="keyword">catch</span> (SecurityException e) &#123;</span><br><span class="line">        Log.w(TAG, <span class="string">&quot;Unable to set the volume controller&quot;</span>, e);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title function_">setVolumeController</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        mAudio.setVolumeController(mVolumeController);</span><br><span class="line">    &#125; <span class="keyword">catch</span> (SecurityException e) &#123;</span><br><span class="line">        Log.w(TAG, <span class="string">&quot;Unable to set the volume controller&quot;</span>, e);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>Audio Manager 就是 Model 层，VolumeDialogControllerImpl 向 Audio Manager 注册了一个回调，其实就是 Presenter 层与 Model 层的关联。</p>
<h4 id="4-音量UI显示"><a href="#4-音量UI显示" class="headerlink" title="4 音量UI显示"></a>4 音量UI显示</h4><p>现在MVP框架已经形成，现在就来分析下当按下 Power 键后，VolumeUI 是如何显示UI的。<br>这里调用AudioManager的setVolumeController方法去设置了音量控制的回调接口：<br><strong>frameworks/base/services/core/java/com/android/server/audio/AudioService.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">AudioService</span> <span class="keyword">extends</span> <span class="title class_">IAudioService</span>.Stub</span><br><span class="line">        <span class="keyword">implements</span> <span class="title class_">AccessibilityManager</span>.TouchExplorationStateChangeListener,</span><br><span class="line">            AccessibilityManager.AccessibilityServicesStateChangeListener &#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="type">VolumeController</span> <span class="variable">mVolumeController</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">VolumeController</span>();</span><br><span class="line">              </span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setVolumeController</span><span class="params">(<span class="keyword">final</span> IVolumeController controller)</span> &#123;</span><br><span class="line">        ...</span><br><span class="line">        mVolumeController.setController(controller);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">class</span> <span class="title class_">VolumeController</span> &#123;</span><br><span class="line">        <span class="keyword">private</span> IVolumeController mController;</span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setController</span><span class="params">(IVolumeController controller)</span> &#123;</span><br><span class="line">            mController = controller;</span><br><span class="line">            mVisible = <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 音量发生改变就会调用这个方法</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">postVolumeChanged</span><span class="params">(<span class="type">int</span> streamType, <span class="type">int</span> flags)</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (mController == <span class="literal">null</span>)</span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                mController.volumeChanged(streamType, flags);</span><br><span class="line">            &#125; <span class="keyword">catch</span> (RemoteException e) &#123;</span><br><span class="line">                Log.w(TAG, <span class="string">&quot;Error calling volumeChanged&quot;</span>, e);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        ...</span><br><span class="line">    &#125;</span><br><span class="line">              </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在AudioService里面定义了一个内部类VolumeController，持有IVolumeController的引用，当音量发生改变就会调用VolumeController的方法，然后调用IVolumeController的方法，最终回调到SystemUI的VolumeDialogControllerImpl的VC类中。<br><strong>frameworks/base/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogControllerImpl.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Singleton</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">VolumeDialogControllerImpl</span> <span class="keyword">implements</span> <span class="title class_">VolumeDialogController</span>, Dumpable &#123;</span><br><span class="line">    ...</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">class</span> <span class="title class_">VC</span> <span class="keyword">extends</span> <span class="title class_">IVolumeController</span>.Stub &#123;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">volumeChanged</span><span class="params">(<span class="type">int</span> streamType, <span class="type">int</span> flags)</span> <span class="keyword">throws</span> RemoteException &#123;</span><br><span class="line">            <span class="comment">// 收到AudioService调用的方法</span></span><br><span class="line">            mWorker.obtainMessage(W.VOLUME_CHANGED, streamType, flags).sendToTarget();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  </span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">class</span> <span class="title class_">W</span> <span class="keyword">extends</span> <span class="title class_">Handler</span> &#123;</span><br><span class="line">        <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">int</span> <span class="variable">VOLUME_CHANGED</span> <span class="operator">=</span> <span class="number">1</span>;</span><br><span class="line">        W(Looper looper) &#123;</span><br><span class="line">            <span class="built_in">super</span>(looper);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">handleMessage</span><span class="params">(Message msg)</span> &#123;</span><br><span class="line">            <span class="keyword">switch</span> (msg.what) &#123;</span><br><span class="line">                <span class="keyword">case</span> VOLUME_CHANGED: onVolumeChangedW(msg.arg1, msg.arg2); <span class="keyword">break</span>;</span><br><span class="line">                ...</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  </span><br><span class="line">    <span class="type">boolean</span> <span class="title function_">onVolumeChangedW</span><span class="params">(<span class="type">int</span> stream, <span class="type">int</span> flags)</span> &#123;</span><br><span class="line">        <span class="keyword">final</span> <span class="type">boolean</span> <span class="variable">showUI</span> <span class="operator">=</span> shouldShowUI(flags);</span><br><span class="line">        <span class="keyword">final</span> <span class="type">boolean</span> <span class="variable">fromKey</span> <span class="operator">=</span> (flags &amp; AudioManager.FLAG_FROM_KEY) != <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">final</span> <span class="type">boolean</span> <span class="variable">showVibrateHint</span> <span class="operator">=</span> (flags &amp; AudioManager.FLAG_SHOW_VIBRATE_HINT) != <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">final</span> <span class="type">boolean</span> <span class="variable">showSilentHint</span> <span class="operator">=</span> (flags &amp; AudioManager.FLAG_SHOW_SILENT_HINT) != <span class="number">0</span>;</span><br><span class="line">        <span class="type">boolean</span> <span class="variable">changed</span> <span class="operator">=</span> <span class="literal">false</span>;</span><br><span class="line">        <span class="keyword">if</span> (showUI) &#123;</span><br><span class="line">            changed |= updateActiveStreamW(stream);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="type">int</span> <span class="variable">lastAudibleStreamVolume</span> <span class="operator">=</span> getAudioManagerStreamVolume(stream);</span><br><span class="line">        changed |= updateStreamLevelW(stream, lastAudibleStreamVolume);</span><br><span class="line">        changed |= checkRoutedToBluetoothW(showUI ? AudioManager.STREAM_MUSIC : stream);</span><br><span class="line">        <span class="keyword">if</span> (changed) &#123;</span><br><span class="line">            <span class="comment">// 调用mCallbacks的onStateChanged方法</span></span><br><span class="line">            mCallbacks.onStateChanged(mState);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (showUI) &#123;</span><br><span class="line">            <span class="comment">// UI 更新</span></span><br><span class="line">            mCallbacks.onShowRequested(Events.SHOW_REASON_VOLUME_CHANGED);</span><br><span class="line">        &#125;</span><br><span class="line">        ...</span><br><span class="line">        <span class="keyword">return</span> changed;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这里的mWork是通过子线程的Looper去初始化的，所以onVolumeChangedW也是在子线程执行的，那么我们mCallbacks的方法也是在子线程执行的，这里的分析也是和上面的第2小点的分析对应上了。<br>根据 flags 决定要执行哪个回调，如果要显示UI，就会回调 onShowRequested() , 而这个回调当然是由 View 层实现的。<br><strong>frameworks/base/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogImpl.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">final</span> VolumeDialogController.<span class="type">Callbacks</span> <span class="variable">mControllerCallbackH</span></span><br><span class="line">        <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">VolumeDialogController</span>.Callbacks() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onShowRequested</span><span class="params">(<span class="type">int</span> reason)</span> &#123;</span><br><span class="line">        showH(reason);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">showH</span><span class="params">(<span class="type">int</span> reason)</span> &#123;</span><br><span class="line">    <span class="comment">// 显示Dialog</span></span><br><span class="line">    mDialog.show();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>View 层就完成了一个 Dialog 的显示。</p>
<h4 id="5-VolumeUI小结"><a href="#5-VolumeUI小结" class="headerlink" title="5 VolumeUI小结"></a>5 VolumeUI小结</h4><p>这里我们来分析一下VolumeUI整理流程：</p>
<ol>
<li>VolumeUI持有VolumeDialogComponent的引用，在调用VolumeUI的start方法时，会判断音量条和安全音量提示是否打开，然后会去注册AudioService的监听。</li>
<li>VolumeDialogComponent的构造函数会去创建音量条实例-VolumeDialogImpl，同时VolumeDialogImpl会去执行一些初始化的操作，同时添加VolumeDialogControllerImpl的监听回调。</li>
<li>注册AudioService的监听是在VolumeDialogControllerImpl里面注册的，当AudioService进行了调整音量的操作后，VolumeDialogControllerImpl会收到通知，同时会将收到的消息回调给VolumeDialogImpl，做出相应的UI调整，这样就完成了一轮操作。</li>
</ol>

      
      <blockquote>
        
        <strong>本文链接：</strong><br><a href="http://longzhiye.top/2024/01/07/2024-01-07/">http://longzhiye.top/2024/01/07/2024-01-07/</a>
      </blockquote>
    </div>
    <footer class="mdui-card-actions">
      
        <a class="mdui-ripple article_categories-link" href="/categories/%E6%8A%80%E6%9C%AF/">技术</a>
      
      
        <a class="mdui-ripple article_tags-link" href="/tags/Android/" rel="tag">Android</a><a class="mdui-ripple article_tags-link" href="/tags/Launcher/" rel="tag">Launcher</a><a class="mdui-ripple article_tags-link" href="/tags/Settings/" rel="tag">Settings</a><a class="mdui-ripple article_tags-link" href="/tags/framework/" rel="tag">framework</a>
      
    </footer>
    
  </article>
  
<script src="//cdn.bootcdn.net/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js"></script>

  <script>$("#main article .mdui-card-content img.fancybox").on("click",function(e){$.fancybox.open({src:$(this).attr("src")});});</script>


  <nav id="paginator">
    
      <a rel="prev" class="extend prev" href="/2024/01/13/2024-01-13/">
        <button aria-label="prev" class="mdui-btn mdui-btn-raised mdui-btn-icon mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_back</i></button>
        <span class="mdui-p-x-3" mdui-tooltip="{content: 'Android APP启动流程解析'}">上一篇</span>
      </a>
    
    <div class="spacer"></div>
    
      <a rel="next" class="extend next" href="/2024/01/06/2024-01-06/">
        <span class="mdui-p-x-3" mdui-tooltip="{content: 'Android 10 Android音量调节'}">下一篇</span>
        <button aria-label="next" class="mdui-btn mdui-btn-raised mdui-btn-icon mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_forward</i></button>
      </a>
    
  </nav>





</main>
  <footer id="footer" class="mdui-m-t-5 mdui-p-y-3 mdui-color-theme">
  <div class="mdui-p-y-0 mdui-text-center">
    
    
    
    
    
    
    
      <a href="http://github.com/longzhiye" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe7ab;</i></a>
    
    
    
      <a href="https://www.zhihu.com/people/long-zhi-xie-61" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe6c0;</i></a>
    
    
    
      <a href="tencent://tencent:////message/?uin=951898105&Menu=yes" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe651;</i></a>
    
    
  </div>
  <div class="mdui-p-y-1 mdui-text-center">
    Copyright &copy; 2022 - 2025 龙之叶<br>
    Powered by <a href="https://hexo.io/" target="_blank" class="mdui-text-color-theme-accent">Hexo</a>
    <br>
      <span id="busuanzi_container_site_pv" style="display: none;"><i class="iconfont">&#xe7fd;</i> <span id="busuanzi_value_site_pv"></span></span> &nbsp;&nbsp;
      <span id="busuanzi_container_site_uv" style="display: none;"><i class="iconfont">&#xe601;</i> <span id="busuanzi_value_site_uv"></span></span>
    
  </div>
</footer>

  <button id="gotop" class="mdui-fab mdui-fab-fixed mdui-fab-hide mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_upward</i></button>
  <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
  
<script src="/js/mdui.js"></script>
<script src="/js/script.js"></script>

  
<script src="/custom.js"></script>

</body>
</html>
